package com.lc.user.express.my;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.view.PayKeyboardPopupWindow;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/3/15. 重置支付密码 设置支付密码
 */
public class SetPayPasswordActivity extends BaseSecondActivity {
    public static final int TYPE_SET = 0x99;//设置支付密码
    public static final int TYPE_RESET = 0x98;//重置支付密码
    public static final int TYPE_FORGET = 0x97;//忘记支付密码
    private LinearLayout ll_container;
    private ArrayList<String> mList = new ArrayList<String>();
    private TextView tv_dic;//描述
    private PayKeyboardPopupWindow popup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pay_password);
        initView();
    }

    private void initView() {

        setTitle(getIntent().getFlags() == TYPE_RESET || getIntent().getFlags() == TYPE_FORGET?"重置支付密码":"设置支付密码");
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        ll_container.setOnClickListener(this);
        tv_dic = (TextView)findViewById(R.id.tv_dic);
        tv_dic.setText("设置6位数字支付密码");
    }
    @Override
    protected void onResume() {
        super.onResume();
        mList.clear();
        ll_container.removeAllViews();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(popup != null)popup.dismiss();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_container:
                showPasswordPopupWindow();
                break;
        }
    }

    private void showPasswordPopupWindow() {
        popup = new PayKeyboardPopupWindow(this);
        popup.showAtLocation(findViewById(R.id.ll_root), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        popup.setOnInputFinishListener(new PayKeyboardPopupWindow.OnInputFinish() {
            @Override
            public void onInput(String password) {
                if (mList.size() <= 0) {
                    addTextView(SetPayPasswordActivity.this, 1, password);
                    mList.add(password);
                } else if (mList.size() == 5) {
                    addTextView(SetPayPasswordActivity.this, 1, password);
                    mList.add(password);
                    StringBuffer sb = new StringBuffer();
                    for (String item : mList) {
                        sb.append(item);
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("pwd", sb.toString());
                    popup.dismiss();
                    if(getIntent().getFlags() == TYPE_SET){
                        bundle.putString("name",getIntent().getStringExtra("name"));
                        bundle.putString("bankcard_num",getIntent().getStringExtra("bankcard_num"));
                        bundle.putString("mobile",getIntent().getStringExtra("mobile"));
                        bundle.putString("bank",getIntent().getStringExtra("bank"));
                        show(SurePayPasswordActivity.class,SurePayPasswordActivity.TYPE_SET, bundle);
                    }else if(getIntent().getFlags() == TYPE_RESET){
                        bundle.putString("oldpwd",getIntent().getStringExtra("oldpwd"));
                        show(SurePayPasswordActivity.class, SurePayPasswordActivity.TYPE_RESET, bundle);
                    }else if(getIntent().getFlags() == TYPE_FORGET){
                        bundle.putString("oldpwd",getIntent().getStringExtra("oldpwd"));
                        show(SurePayPasswordActivity.class, SurePayPasswordActivity.TYPE_FORGET, bundle);
                    }

                } else if (mList.size() < 6) {
                    addTextView(SetPayPasswordActivity.this, 0, password);
                    mList.add(password);
                }
            }

            @Override
            public void onDelete() {
                if (mList.size() > 0) {
                    ll_container.removeViewAt(mList.size() - 1);
                    mList.remove(mList.get(mList.size() - 1));
                }
            }
        });

    }

    private void addTextView(Activity context, int type, String s) {
        LinearLayout ll = (LinearLayout) View.inflate(context, R.layout.item_key, null);
        if (type == 1) {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(116),// 两侧的view稍宽
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        } else {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(110),
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        }

        AppContext.ScaleScreenHelper.loadView(ll);

        TextView textView = (TextView) ll.findViewById(R.id.tv_key);
        textView.setText(s);

        ll_container.addView(ll);
    }

}

